//记录当前选择的“课程周期”
var electiveWeek = 0;

//记录当前选择的“课程几次”
var electiveTime = 0;

var adminClaszs;
var studyClaszs;

$(function () {

    loadAdminStudyClasz();

    $("#course_table").datagrid({
        fit: true,
        rownumbers: false,
        singleSelect: true,
        url: "study/list/attendance/course/table",
        pagination: false,
        title: "选课课表",
        columns: [
            [
                {field: "courseTime", width: 100, align: 'center', title: '课程节次', rowspan: 2},
                {align: 'center', title: '课程周期', colspan: 7}
            ],
            [
                {field: "mondayCourse", width: 100, align: 'center', title: '周一'},
                {field: "tuesdayCourse", width: 100, align: 'center', title: '周二'},
                {field: "wednesdayCourse", width: 100, align: 'center', title: '周三'},
                {field: "thursdayCourse", width: 100, align: 'center', title: '周四'},
                {field: "fridayCourse", width: 100, align: 'center', title: '周五'},
                {field: "saturdayCourse", width: 100, align: 'center', title: '周六'},
                {field: "sundayCourse", width: 100, align: 'center', title: '周日'}
            ]
        ],
        onClickCell: courseTableClickCell,
        onClickRow: function (rowIndex, rowData) {
            var panel = $('#course_table').datagrid('getPanel');
            var tr = panel.find('div.datagrid-body tr[datagrid-row-index="' + rowIndex + '"]');
            tr.removeClass("datagrid-row-selected");
        }
    });

    $("#attendance_situation").datagrid({
        fit: true,
        rownumbers: true,
        singleSelect: true,
        pagination: true,
        pageSize: 10,
        title: "未选课的学生列表",
        columns: [
            [
                {field: 'studentNumber', title: '学号', align: 'center', width: 50},
                {field: 'name', title: '姓名', align: 'center', width: 80},
                {field: 'sex', title: '性别', align: 'center', width: 40,
                    formatter: function (value, row, index) {
                        return sex[value];
                    }
                },
                {field: 'identityCardNumber', title: '身份证号码', align: 'center', width: 150},
                {field: 'birthday', title: '生日', align: 'center', width: 80},
                {field: 'nation', title: '民族', align: 'center', width: 60,
                    formatter: function (value, row, index) {
                        return nation[value - 1];
                    }
                },
                {field: 'executiveClass', title: '所在行政班', align: 'center', width: 100,
                    formatter: function (value, row) {
                        if (value > 0) {
                            for (var i = 0; i < adminClaszs.length; i++) {
                                if (adminClaszs[i].id == value) {
                                    return adminClaszs[i].name;
                                }
                            }
                        } else
                            return '';
                    }
                },
                {field: 'teachingClass', title: '所在教学班', align: 'center', width: 100,
                    formatter: function (value, row) {
                        if (value > 0) {
                            for (var i = 0; i < studyClaszs.length; i++) {
                                if (studyClaszs[i].id == value) {
                                    return studyClaszs[i].name;
                                }
                            }
                        } else
                            return '';
                    }
                }
            ]
        ],
        toolbar: "#tb",
        loadFilter: pageLoadFilter
    });

    $("#refresh").linkbutton({
        iconCls: "icon-reload",
        plain: true
    }).click(function () {
            if (timeTitle != undefined) {
                $("#attendance_situation").datagrid("loadData", []);
                $("#attendance_situation").datagrid("options").pageNumber = 1;
                $("#attendance_situation").datagrid('getPager').pagination({pageNumber: 1});
                $("#attendance_situation").datagrid({
                    title: weekTitle + "-" + timeTitle + "-未选课的学生列表",
                    url: "study/list/no/election/" + electiveWeek + "/" + electiveTime + "/students"
                });
            }
        });

    $("#search_grade").searchbox({
        searcher: function (value, name) {
            if (timeTitle != undefined) {
                $("#attendance_situation").datagrid("loadData", []);
                $("#attendance_situation").datagrid("options").pageNumber = 1;
                $("#attendance_situation").datagrid('getPager').pagination({pageNumber: 1});

                $("#attendance_situation").datagrid({
                    title: weekTitle + "-" + timeTitle + "-未选课的学生列表",
                    url: "study/list/no/election/" + electiveWeek + "/" + electiveTime + "/students/grade/",
                    queryParams: {
                        grade: value
                    }
                });
            }
        },
        prompt: '请输入年级'
    });

});

var timeTitle = undefined;
var weekTitle = undefined;

function courseTableClickCell(rowIndex, field, value) {
    if (field == "courseTime") {
        return;
    }

    var panel = $('#course_table').datagrid('getPanel');
    var selectTd = panel.find('div.datagrid-body tr td.datagrid-row-selected');
    selectTd.removeClass("datagrid-row-selected");
    var tr = panel.find('div.datagrid-body tr[datagrid-row-index="' + rowIndex + '"]');
    tr.each(function () {
        //添加选择的单元格样式
        var td = tr.children('td[field="' + field + '"]');
        td.addClass("datagrid-row-selected");
    });

    electiveWeek = {
        mondayCourse: 1,
        tuesdayCourse: 2,
        wednesdayCourse: 4,
        thursdayCourse: 8,
        fridayCourse: 16,
        saturdayCourse: 32,
        sundayCourse: 64
    }[field] || 0;
    weekTitle = {
        mondayCourse: "周一",
        tuesdayCourse: "周二",
        wednesdayCourse: "周三",
        thursdayCourse: "周四",
        fridayCourse: "周五",
        saturdayCourse: "周六",
        sundayCourse: "周日"
    }[field] || "";
    timeTitle = $("#course_table").datagrid("getRows")[rowIndex]["courseTime"];
    electiveTime = {
        "第一节课": 1,
        "第二节课": 2,
        "第三节课": 4,
        "第四节课": 8,
        "第五节课": 16,
        "第六节课": 32,
        "第七节课": 64,
        "第八节课": 128
    }[timeTitle] || 0;

    $("#attendance_situation").datagrid("loadData", []);
    $("#attendance_situation").datagrid("options").pageNumber = 1;
    $("#attendance_situation").datagrid('getPager').pagination({pageNumber: 1});

    if (timeTitle != undefined) {
        $("#attendance_situation").datagrid("loadData", []);
        $("#attendance_situation").datagrid("options").pageNumber = 1;
        $("#attendance_situation").datagrid('getPager').pagination({pageNumber: 1});
        $("#attendance_situation").datagrid({
            title: weekTitle + "-" + timeTitle + "-未选课的学生列表",
            url: "study/list/no/election/" + electiveWeek + "/" + electiveTime + "/students"
        });
    }

//    $.ajax({
//        type: "GET",
//        url: "study/list/no/election/" + electiveWeek + "/" + electiveTime + "/students",
//        dataType: "json",
//        cache: false,
//        success: function (data) {
//            $("#attendance_situation").datagrid("getPanel").panel("setTitle", weekTitle + "-" + timeTitle + "-未选课的学生列表");
//
//            if (data) {
//                $("#attendance_situation").datagrid("loadData", data);
//            } else {
//                $("#attendance_situation").datagrid("loadData", []);
//            }
//
//        }
//    });
};

function loadAdminStudyClasz() {
    $.getJSON('clasz/all-admin-claszs', function (data) {
        adminClaszs = data;
    });
    $.getJSON('clasz/all-study-claszs', function (data) {
        studyClaszs = data;
    });
}